
package client;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * Initializes and run the client application. It creates a JFrame and adds a 
 * LoginPanel to it.
 *
 * @author xissburg
 */
public class ClientApp implements Runnable
{
    private JFrame frame;
    private int windowWidth, windowHeight;
    private GameController gameController;
    
    public ClientApp(int windowWidth, int windowHeight) {
        this.windowWidth = windowWidth;
        this.windowHeight = windowHeight;
        gameController = new GameController();
    }

    public void quit()
    {
        gameController.logout();
    }
    
    public void run()
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() 
            {
                try {
                    UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel");
                } catch (Exception e) {
                    System.out.println("Substance Raven failed to initialize");
                }

                JFrame.setDefaultLookAndFeelDecorated(true);
                frame = new JFrame("Dynamic Button Soccer");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setMinimumSize(new Dimension(640, 480));
                frame.setLocationRelativeTo(null);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e)
                    {
                        quit();
                    }
                });
                
                LoginPanel loginPanel = new LoginPanel(gameController);
                gameController.setEventHandler(loginPanel);
                frame.getContentPane().setMinimumSize(new Dimension(32, 32));
                frame.add(loginPanel);
                frame.setSize(windowWidth, windowHeight);
                frame.setVisible(true);
            }
        });
    }
}
